package tr.com.netgis.util;

/**
 * 
1 Konum Bildirme Aralığı Ayarla Set Device Tracking Time
2 Konum Bilgisi Al Get Position Info
3 Uygulamayı Kapat Kill Application
4 Cihazı Kapat Turn Off Device
5 Hedef Koordinat Bilgisi Gönder Send Target Coordinate Info
6 Rota Bilgisi Gönder Send Route GML
7 Kaydedilecek Konum Sayısını Ayarla Set Number of Positions to Be Store
 * 
 * @author NetGis
 *
 */
public enum Command {
	
	
	SEND_FREQUENCY (1, true), //used in
	GET_LOCATION_INFO (2, false),
	KILL_APPLICATION (3, false),
	TURN_OFF_DEVICE (4, false),
	SEND_TARGET_COORDINATE_INFO (5, false),
	SEND_ROUTE_GML (6, false),
	LOCATION_SIZE (7, true), //used in
	
	/**
	 * possible values are 500, 200, 100, 50, 1
	 */
	MIN_TIME (8, true), //used in
	MIN_DISTANCE (9, true), //used in
	UNKNOWN (-99, false);
	
	private int code;
	private boolean usedForDevice;

	private Command(int code, boolean usedForDevice) {
		this.code = code;
		this.usedForDevice = usedForDevice;
	}
	
	public boolean isUsedForDevice() {
		return usedForDevice;
	}


	public int getCode() {
		return code;
	}
	
	public static Command valueOf (int code) {
		for (Command command: Command.values()) {
			if (command.getCode() == code)
				return command;
		}
		return UNKNOWN;
	}
	
	/**
	 *     
    <string-array name="listLocSizeEntryValues">
        <item>20</item>
        <item>15</item>
        <item>10</item>
        <item>5</item>
    </string-array>

    <string-array name="listSendFreqEntryValues">
        <item>10</item>
        <item>9</item>
        <item>8</item>
        <item>7</item>
        <item>6</item>
        <item>5</item>
        <item>4</item>
        <item>3</item>
        <item>2</item>
        <item>1</item>
    </string-array>
    
    <string-array name="listMinDistanceEntryValues">
        <item>3000</item>
        <item>1000</item>
        <item>100</item>
        <item>10</item>
        <item>1</item>
        <item>0</item>
    </string-array>
    
    <string-array name="listMinTimeEntryValues">
        <item>500</item>
        <item>200</item>
        <item>100</item>
        <item>50</item>
        <item>1</item>
    </string-array>
     * <br>
     * Validates command by valueStr
	 * @param command
	 * @param valueStr
	 * @return
	 * @throws Exception if command is not valid for this device
	 */
	public static int validate (Command command, String valueStr) throws Exception {
		int value = Utility.toInt(valueStr);
		if (Command.LOCATION_SIZE.equals(command)) {
			if (value < 5 || value > 20) {
				return Constants.LOCATION_SIZE;
			} else {
				if (value >= 5 && value < 10)
					return 5;
				if (value >= 10 && value < 15)
					return 10;
				if (value >= 15 && value < 20)
					return 15;
				return 20;
			}
		} else if (Command.SEND_FREQUENCY.equals(command)) {
			float fValue = 0;
			try {
				fValue = Float.parseFloat(valueStr);
				return (int)(fValue * 60);
			} catch (Exception e) {
			}
			
			if (value < 1 || value > 10) {
				return Constants.SEND_FREQUENCY;
			}
			return value;
		} else if (Command.MIN_DISTANCE.equals(command)) {
			if (value < 0 || value > 3000) {
				return Constants.MINIMUM_DISTANCE_CHANGE_FOR_UPDATES;
			}
			if (value == 0)
				return 0;
			if (value == 1)
				return 1;
			if (value >= 10 && value < 100)
				return 10;
			if (value >= 100 && value < 1000)
				return 100;
			if (value >= 1000 && value < 3000)
				return 1000;
			return 3000;
		} else if (Command.MIN_TIME.equals(command)) {
			if (value < 1 || value > 500) {
				return Constants.MINIMUM_TIME_BETWEEN_UPDATES;
			}
			if (value >= 1  && value < 50)
				return 1;
			if (value >= 50  && value < 100)
				return 50;
			if (value >= 100  && value < 200)
				return 100;
			if (value >= 200  && value < 500)
				return 200;
			return 500;
		}
		
		throw new Exception(command + " not valid");
	}
	

	
}
